import { SiteConfig } from '@libs/common/types/configs';

import { splitCase } from '../utils/string.utils';

class ConfigCenter {
    protected getConfig<T = any>(configName: string): T {
        let value: T = undefined;

        // use webpack dependency analyze to avoid import serverside code
        if (process.env.BROWSER) {
            value = window?.[`__${splitCase(configName)?.toUpperCase()}__`];
        } else {
            const serverSideConfig = require('./config-center.server-side');
            value = new serverSideConfig.ConfigCenterServerSide().getConfig(splitCase(configName, '-')?.toLowerCase());
        }

        return value ?? ({} as any);
    }

    get siteConfig(): SiteConfig {
        return this.getConfig<SiteConfig>('SiteConfig');
    }
}

const ConfigCenterSingleton = new ConfigCenter();

export { ConfigCenterSingleton };
